import { useMessage } from '/@/hooks/web/useMessage';

function twoDecimal(number) {
  const roundedNumber = Math.round(number * 100) / 100; // 先保留两位小数
  const numString = roundedNumber.toString();
  const decimalIndex = numString.indexOf('.');

  if (decimalIndex !== -1) {
    const len = numString.length - decimalIndex - 1;
    if (len > 2) {
      return parseFloat(numString.substring(0, decimalIndex + 3));
    }
  }
  return roundedNumber;
}

/**
 * 
 * @param type 第三方 1
 * @param data 
 * @param old 
 * @param nowAmount 
 * @returns 
 */
const countStatus = ((type=1, data, old, nowAmount) => {
  console.log('old', old, data)
  let oldObj = old ? JSON.parse(old) : {}
  const { createMessage } = useMessage()
  if(!data.length) return

  // currentAvablieCost;//当前可用
  // remainderBudget;//当月剩余

  if(!nowAmount) {
    console.log('当前值：'+ nowAmount, oldObj)
    data[0].remainderBudget = oldObj.remainderBudget
    data[0].historyRemainder = oldObj.historyRemainder
    data[0].currentAvablieCost = oldObj.currentAvablieCost
    return
  }
  if(Number(oldObj.remainderBudget) - Number(nowAmount) > 0 ){
    // 直接扣除
    console.log(5,'remainderBudget', oldObj.remainderBudget, nowAmount, Number(oldObj.remainderBudget) - Number(nowAmount))
    data[0].remainderBudget = twoDecimal(Number(oldObj.remainderBudget) - Number(nowAmount))
    console.log('data[0].remainderBudget',data[0].remainderBudget)
  }

  if(Number(oldObj.remainderBudget) - Number(nowAmount) < 0  &&  data[0].historyRemainder + Number(oldObj.remainderBudget) - Number(nowAmount) > 0){
    // 直接扣除
    data[0].remainderBudget = 0;
    data[0].historyRemainder = twoDecimal(data[0].historyRemainder - ( Number(nowAmount) - oldObj.remainderBudget))

  }


/*    if(data[0].historyRemainder <=0) {
      console.log(32232,oldObj)
      data[0].remainderBudget = Number(oldObj.remainderBudget) - Math.abs(Number(oldObj.historyRemainder) - Number(nowAmount))//oldObj.remainderBudget
    }else {
      data[0].remainderBudget = oldObj.remainderBudget
    }
  }else if(Number(data[0].currentAvablieCost)  - Number(nowAmount) < 0 && Number(data[0].historyRemainder) - (Number(nowAmount) - Number(data[0].currentAvablieCost)) > 0){
    console.log('重新赋值')
    data[0].currentAvablieCost = 0//Number(oldObj.currentAvablieCost) - Number(nowAmount) <=0 ? 0 : Number(oldObj.currentAvablieCost) - Number(nowAmount)
    data[0].historyRemainder = Number(oldObj.historyRemainder) - (Number(nowAmount) - Number(oldObj.currentAvablieCost))
    return false
  }*/

  if((Number(oldObj.historyRemainder) + Number(oldObj.remainderBudget)) - Number(nowAmount) < 0){
    // 提示无法扣除
    createMessage.warn('超出预算限额，无法申报')
    data[0].remainderBudget = oldObj.remainderBudget
    data[0].historyRemainder = oldObj.historyRemainder
    data[0].currentAvablieCost = oldObj.currentAvablieCost
    return true
  }
})

function dateMonths(date1, date2) {
  // 将日期字符串转换为Date对象
  const startDate = new Date(date1);
  const endDate = new Date(date2);

  // 差异
  const timeDif = endDate.getTime() - startDate.getTime();
  const daysDiff = Math.floor(timeDif / (1000 * 60 * 60 * 24));

  // 计算月份差异
  const monthDiff = (endDate.getFullYear() - startDate.getFullYear()) * 12 +
      (endDate.getMonth() - startDate.getMonth());

  // 如果日期差异超过半个月，则按整月计算
  if (daysDiff >= 15) {
      return monthDiff + 1; // 超过半月，按整月计算
  } else if (daysDiff > 0) {
      return monthDiff + 0.5; // 不满半月，按半月计算
  } else {
      return monthDiff; // 没有日期差异
  }
}

function notEmpty(obj, properties) {
  for (const prop of properties) {
    if (!(prop in obj) || obj[prop] === '' || obj[prop] === null || obj[prop] === undefined || (Array.isArray(obj[prop]) && obj[prop].length === 0)) {
      return true
    }
  }
  return true;
}

// 第三方
const countStatusSupplier = ((type=1, data, old, nowAmount) => {
  // 1、输入金额时当前剩余remainderBudget = remainderBudget - e.Amount（输入金额）
  // 2、项目状态 timePercent >= (totalAmount + e.Amount（输入金额）) /totalCost  则costStatus = 1
  // timePercent < (totalAmount + e.Amount（输入金额）) /totalCost 则costStatus =3 
  let oldObj = old ? JSON.parse(old) : {}
  if(!data.length) return

  if(!nowAmount) {
    console.log('当前值：'+ nowAmount, oldObj)
    data[0].remainderBudget = oldObj.remainderBudget
    data[0].costPercent = oldObj.costPercent
    data[0].totalAmount = oldObj.totalAmount
    return
  }

  data[0].remainderBudget = Number(oldObj.remainderBudget) - Number(nowAmount)
  data[0].totalAmount = Number(oldObj.totalAmount) + Number(nowAmount)
  //  ( totalCost -(totalAmount + e.amount))/totalCost
  // (totalAmount + e.amount)/totalCost 
  data[0].costPercent = (Number(oldObj.totalAmount) + Number(nowAmount)) / Number(oldObj.totalCost)//twoDecimal((Number(oldObj.totalAmount) + Number(nowAmount)) / Number(oldObj.totalCost))
})

// R类人力资源费用申请
const countStatusHr = ((type=1, data, old, nowAmount) => {
  // 1、remainderBudget = remainderBudget - e.Amount（输入金额）
  // 2、可以支配月费 用1的 remainderBudget结果 / monthsDiff 就是后面每个月可支配月费
  
  let oldObj = old ? JSON.parse(old) : {}
  if(!data.length) return
  console.log('data[0].operateType' ,data[0].operaType)

  if(!nowAmount) {
    console.log('当前值：'+ nowAmount, oldObj)
    data[0].remainderBudget = oldObj.remainderBudget
    data[0].currentMonthConsume = oldObj.currentMonthConsume
    data[0].totalAmount = oldObj.totalAmount
    return
  }
  if(data[0].operaType == 3) {
    data[0].remainderBudget =  data[0].remainderBudget
    data[0].totalAmount = data[0].totalAmount
    data[0].currentMonthConsume =  data[0].currentMonthConsume

    return
  }
  data[0].remainderBudget = Number(oldObj.remainderBudget) - Number(nowAmount)
  data[0].totalAmount = Number(oldObj.totalAmount) + Number(nowAmount)
  data[0].currentMonthConsume = twoDecimal(Number(oldObj.monthsDiff) == 0 ? Number(data[0].remainderBudget) : Number(data[0].remainderBudget) / Number(oldObj.monthsDiff))

})

export {
  countStatus,
  countStatusSupplier,
  countStatusHr,
  twoDecimal,
  dateMonths,
  notEmpty
}
